package com.wxyzz.satserver.friend.service.impl;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.wxyzz.common.core.entity.User;
import com.wxyzz.common.core.utils.UserUtils;
import com.wxyzz.satserver.component.OnlineUserManager;
import com.wxyzz.satserver.friend.dao.FriendMapper;
import com.wxyzz.satserver.friend.entity.AddFriendRequest;
import com.wxyzz.satserver.friend.entity.vo.FriendVO;
import com.wxyzz.satserver.friend.service.IFriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxyzz.satserver.user.dao.UserDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyz
 * @since 2024-11-14
 */
@Service
public class FriendServiceImpl implements IFriendService {
    @Resource
    private FriendMapper friendMapper;
    @Resource
    private UserDao userDao;

    @Resource
    private OnlineUserManager onlineUserManager;

    @Resource
    private ObjectMapper objectMapper;



    @Override
    public List<FriendVO> selectFriendList(Integer userId) {
        return friendMapper.selectFriendList(userId);
    }

    @Override
    public List<FriendVO> findFriend(int selfUserId, String name) {

        return friendMapper.findFriend(selfUserId,name);
    }

    @Override
    public void addFriend(int friendId, String reason, HttpServletRequest req) {
        try {
//            HttpSession session = req.getSession(false);
//            User user = (User) session.getAttribute("user");
//            System.out.println("[添加好友] " + user.getId() + ", " + friendId + ", " + reason);
            System.out.println("[添加好友] " + 1 + ", " + friendId + ", " + reason);
            // 写⼊到数据库
//            friendMapper.addFriendRequest(user.getId(), friendId, reason);
            friendMapper.addFriendRequest(1,friendId,reason);
            // 发送 websocket 响应
            WebSocketSession webSocketSession =
                    onlineUserManager.getSession(friendId);

            if (webSocketSession != null) {
                AddFriendRequest addFriendRequest = new AddFriendRequest();
                addFriendRequest.setType("addFriendRequest");
//                addFriendRequest.setFromUserId(user.getId());
                addFriendRequest.setFromUserId(1);
//                addFriendRequest.setFromUserName(user.getUsername());
                addFriendRequest.setFromUserName("admin");
                addFriendRequest.setReason(reason);
                // 给对应的⽤⼾发送 websocket 请求, 提⽰有⽤⼾尝试加好友.
                webSocketSession.sendMessage(new TextMessage(objectMapper.writeValueAsString(addFriendRequest)));
            }
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public  List<AddFriendRequest> getFriendRequest(HttpServletRequest req) {
        List<AddFriendRequest> addFriendRequestList = new ArrayList<>();
        try {
//            HttpSession session = req.getSession(false);
//            User user = (User) session.getAttribute("user");
            User user = userDao.selectById(UserUtils.getUid());
            addFriendRequestList = friendMapper.getFriendRequest(1);
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        return addFriendRequestList;
    }

    @Override
    @Transactional
    public void acceptFriend(int friendId, HttpServletRequest req) {
        try {
//            HttpSession session = req.getSession(false);
//            User user = (User) session.getAttribute("user");
            User user = userDao.selectById(UserUtils.getUid());
//
            //1.删除好友请求表的存档
            friendMapper.deleteFriendRequest(friendId,user.getId());
            //2.修改好友表
            friendMapper.addFriend(friendId,user.getId());
            WebSocketSession webSocketSession = onlineUserManager.getSession(friendId);
            if(webSocketSession != null){
                AddFriendRequest addFriendRequest = new AddFriendRequest();
                addFriendRequest.setType("acceptFriend");
                addFriendRequest.setFromUserId(user.getId());
                addFriendRequest.setFromUserName(user.getUsername());
                addFriendRequest.setReason("");
                webSocketSession.sendMessage(new TextMessage(objectMapper.writeValueAsString(addFriendRequest)));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void rejectFriend(int friendId, HttpServletRequest req) {
        try {
//            HttpSession session = req.getSession(false);
//            User user = (User) session.getAttribute("user");
            User user = userDao.selectById(UserUtils.getUid());
            // 删除好友请求表的存档
            friendMapper.deleteFriendRequest(friendId, user.getId());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

    }


}
